#include "WordInputer.h"

#include "WordCache.h"
#include "WordReport.h"

WordInputer::WordInputer() {
    m_wordCache = WordCache::Create();
    m_wordReport = WordReport::Create();
    m_wordCache->SetReport(m_wordReport);
}

WordInputer::~WordInputer() {
}

void WordInputer::Start() {
    m_thread = new std::thread(&WordInputer::run, this);
    m_thread->join();
}

void WordInputer::run() {
    while (true) {
        std::string word = GetNextWord();
        //std::cout<<"\rWord: "<<word<<std::flush;
        if (word == "esc") {
            m_wordCache->Report();
            break;
        }

        int ret = m_wordCache->AddWord(word);
        switch (ret)
        {
        case WordCache::ADD_ERROR_DUPLICATED:
            std::cerr << "Duplicated word: " << word << std::endl;
            break;
        case WordCache::ADD_ERROR_WRONG:
            std::cerr << "Wrong word: " << word << std::endl;
            break;
        default:
            break;
        }
    }
}

std::string WordInputer::GetNextWord() {
    std::string word;
    std::cin >> word;
    return word;
}